package com.zxr.demo.sub;

import com.zxr.demo.model.Users;
import com.zxr.demo.service.UserService;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RedisMessageSubscriber {
    private static final Logger log = LoggerFactory.getLogger(RedisMessageSubscriber.class);
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    UserService userService;

    @PostConstruct
    public void subscribe() {
        //1.获取主题
        RTopic topic = redissonClient.getTopic("topic-add-user");
        //2.添加监听器，指定消息类型为 String
        topic.addListener(Users.class, new MessageListener<Users>() {
            @Override
            public void onMessage(CharSequence channel, Users msg) {
                //这里是收到信息后的处理逻辑
                log.info("收到来自频道【" + channel + "】的信息：" + msg.toString());
            }
        });
        log.info("已成功订阅频道：aa");

        RTopic topicLogin = redissonClient.getTopic("topic-login-user");
        topicLogin.addListener(Users.class, new MessageListener<Users>() {
            @Override
            public void onMessage(CharSequence channel, Users msg) {
                //这里是收到信息后的处理逻辑
                log.info("收到来自频道【" + msg.getName() + "】登录成功：");
            }
        });
        log.info("已成功登录：topic-login-user");
    }

    }
